package com.maguangwu.server.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maguangwu.dao.CheckitemDao;
import com.maguangwu.entity.PageResult;
import com.maguangwu.entity.QueryPageBean;
import com.maguangwu.exception.MyException;
import com.maguangwu.pojo.CheckItem;
import com.maguangwu.server.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author mgw
 * @create 2021/10/20 23:12
 */
@Service
@Transactional
public class  CheckitemServiceimpl implements CheckitemService {
    @Autowired
    private CheckitemDao checkitemDao;

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItems=  checkitemDao.findAll();
        return checkItems;
    }

    @Override
    public void add(CheckItem checkItem) {
        checkitemDao.add(checkItem);
    }

    @Override
    public PageResult<CheckItem> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
        Page<CheckItem> pages= checkitemDao.findPage(queryPageBean.getQueryString());
        long total = pages.getTotal();
        List<CheckItem> result = pages.getResult();
        return new PageResult<>(total,result);
    }

    @Override
    public CheckItem findById(Integer id) {
       return checkitemDao.findById(id);
    }

    @Override
    public void updateById(CheckItem checkItem) {
        checkitemDao.updateById(checkItem);
    }

    @Override
    public void deleteById(Integer id) {
        Integer total=  checkitemDao.findCheckGroupById(id);
        if (total>0){
            throw new MyException("该检查项已被检查组使用");
        }
        checkitemDao.deleteById(id);
    }
}
